import {useRoutes} from "react-router-dom"
import { Suspense, lazy } from 'react'
import { Spin } from 'antd'
const routes = [
  {
    path: '/',
    auth:true,
    component:lazy(() => import('../View/Login')),
    code:'all',
  },
  {
    path: '/home',
    auth:false,
    component:lazy(() => import('../View/Home')),
    code:'all',
    children: [
      { 
        path: '/home/',
        auth:false,
        component:lazy(() => {
          return import('../View/Admin/Welcome')}),
        code:'all'
      },
      { 
        path: '/home/per_mgr',
        auth:false,
        component:lazy(() => import('../View/Admin/PerMgr')),
        code:'account_mgr'
      },
      { path: '/home/role_mgr',
        auth:false,
        component:lazy(() => import('../View/Admin/RoleMgr')),
        code:'account_mgr'
      },
      { path: '/home/user_mgr',
        auth:false,
        component:lazy(() => import('../View/Admin/UserMgr')),
        code:'account_mgr'
      },
      { path: '/home/goods_mgr',
        auth:false,
        component:lazy(() => import('../View/Admin/GoodsMgr')),
        code:'shops_mgr'
      },
      { path: '/home/order_mgr',
        auth:false,
        component:lazy(() => import('../View/Admin/OrderMgr')),
        code:'shops_mgr'
      },
      { path: '/home/goods_count',
        auth:false,
        component:lazy(() => import('../View/Admin/GoodsCount')),
        code:'sell_count'
      },
      { path: '/home/order_count',
        auth:false,
        component:lazy(() => import('../View/Admin/OrderCount')),
        code:'sell_count'
      },
      { path: '/home/personal_set',
        auth:false,
        component:lazy(() => import('../View/Admin/PersonalSet')),
        code:'all',
        children:[
          {
            path:'/home/personal_set/',
            auth:false,
            component:lazy(()=>import('../View/Admin/PersonalSet/Basic_Set.js')),
            code:'all',
          },
          {
            path:'/home/personal_set/safe_set',
            auth:false,
            component:lazy(()=>import('../View/Admin/PersonalSet/Safe_Set.js')),
            code:'all',
          },          
        ]
      }
    ]
  },
  { 
    path: '/home/personal_set/*',
    auth:true,
    component:lazy(() => import('../View/Empty')),
    code:'all'
  },
  { 
    path: '/home/*',
    auth:true,
    component:lazy(() => import('../View/Empty')),
    code:'all',
  },
  { 
    path: '/*',
    auth:true,
    component:lazy(() => import('../View/Empty')),
    code:'all',
  }
]
//根据路径获取路由
const checkAuth = (routers, path)=>{
  for (const data of routers) {
    if (data.path===path) return data
    if (data.children) {
      const res = checkAuth(data.children, path)
      if (res) return res
    }
  }
  return null
}
// 路由处理方式
const  generateRouter = (routers) => {
  return routers.map((item) => {
    if (item.children) {
      item.children = generateRouter(item.children)
    }

    item.element = <Suspense fallback={
      <div className="example">
        <Spin tip="Loading..."/>
      </div>
    }>
      {/* 把懒加载的异步路由变成组件装载进去 */}
      <item.component/>
    </Suspense>
    return item
  })
}
const Router = () => useRoutes(generateRouter(routes))
const checkRouterAuth = (path)=>{
  let auth = null
  auth = checkAuth(routes,path)
  return auth
}
export{ Router,checkRouterAuth}